﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace homeIMSService.Model
{
    //class represents a valid user that is logged in. contains information necessary for querying db for assets
    class user:DBconnection.dbConnector
    {
        //private variables that store user information
        private string _userName;
        private string _password;
        private int _userID;
        
        //public variables for access
        public int UserID
        {
            get { return _userID;  }
        }
        //constructor used to hydrate class
        public user(string userName, string password, int userID)
        {
            //set private variables using params
            _userName = userName;
            _password = password;
            _userID = userID;
        }

        //method used to retieve all information about user and send it to client
        public userInfo getAllInfo()
        {
            //create SqlParameters for db query
            List<SqlParameter> liSqlParams = new List<SqlParameter>();
            SqlParameter uID = new SqlParameter("@userID", _userID);
            liSqlParams.Add(uID);

            //access db for all information
            DataTable dtResults = null;
            dtResults = SendDataTableRequest("dbo.spGetSingleUser", liSqlParams);

            //store information in data members
            userInfo newUserInfo = new userInfo();

            foreach (DataRow row in dtResults.Rows)
            {
                newUserInfo.userID = Convert.ToInt32(row["PKuserID"]);
                newUserInfo.userName = row["UserName"].ToString();
                newUserInfo.password = row["UserPassword"].ToString();
                newUserInfo.address = row["Address"].ToString();
                newUserInfo.city = row["City"].ToString();
                newUserInfo.state = row["State"].ToString();
                newUserInfo.zip = row["zip"].ToString();
            }
            //return datatable for use
            return newUserInfo;
        }

        //method for receiving all information about user and updating through dbConnector
        public bool updateAllInfo(userInfo userInformation)
        {
            //create SqlParameters for db query
            List<SqlParameter> liSqlParams = new List<SqlParameter>();

            //take each string from userInformation and store it as SqlParameter
            SqlParameter uID = new SqlParameter("@userID", userInformation.userID);
            SqlParameter uName = new SqlParameter("@userName", userInformation.userName);
            SqlParameter uPass = new SqlParameter("@userPass", userInformation.password);
            SqlParameter address = new SqlParameter("@address", userInformation.address);
            SqlParameter city = new SqlParameter("@city", userInformation.city);
            SqlParameter state = new SqlParameter("@state", userInformation.state);
            SqlParameter zip = new SqlParameter("@zip", Convert.ToInt32(userInformation.zip));
            SqlParameter phone = new SqlParameter("@phoneNum", Convert.ToInt32(userInformation.phoneNumber));

            //add SqlParameters to liSqlParams
            liSqlParams.Add(uID);
            liSqlParams.Add(uName);
            liSqlParams.Add(uPass);
            liSqlParams.Add(address);
            liSqlParams.Add(city);
            liSqlParams.Add(state);
            liSqlParams.Add(zip);
            liSqlParams.Add(phone);

            //store information using dbConnector
            int result = SendWriteRequest("spUpdateUser", liSqlParams);
            bool updated = false ;
            //switch statement to send result of update
            switch (result)
            {
                case 0:
                        updated = false;
                        break;
                case 1:
                        updated = true;
                        break;   
            }

            //return result
            return updated;
        }

        //method for retrieving all asset information for user
        public assetList getUserAssets()
        {

            //create SqlParameters for db query
            List<SqlParameter> liSqlParams = new List<SqlParameter>();
            SqlParameter uID = new SqlParameter("@userID", _userID);
            liSqlParams.Add(uID);

            //access db for all information
            DataTable dtResults = null;
            dtResults = SendDataTableRequest("dbo.spGetUserAssets", liSqlParams);

            //store information in data members
            assetList newAssetList = new assetList();
            List<assetInfo> newUserAssets = new List<assetInfo>();

            foreach (DataRow row in dtResults.Rows)
            {
                //for each asset, create new assetInfo and store into list
                assetInfo newAsset = new assetInfo();

                //store assetInformation that have only one part
                newAsset.assetID = Convert.ToInt32(row["PKassetID"]);
                newAsset.userID = Convert.ToInt32(row["FKuserID"]);
                newAsset.typeID = Convert.ToInt32(row["PKtypeLU"]);
                newAsset.type = row["type"].ToString();
                newAsset.name = row["Name"].ToString();
                newAsset.manufacturer = row["Manufacturer"].ToString();
                newAsset.qualityID = Convert.ToInt32(row["PKqualityLU"]);
                newAsset.quality = row["Quality"].ToString();
                newAsset.IDNumber = row["IDNumber"].ToString();
                newAsset.make = row["Make"].ToString();
                newAsset.model = row["Model"].ToString();
                newAsset.comments = row["Comments"].ToString();
                newAsset.retailValue = Convert.ToInt32(row["RetailValue"]);
                newAsset.quantity = Convert.ToInt32(row["Quantity"]);
                newAsset.createDate = Convert.ToDateTime(row["CreateDate"]);
                newAsset.lastUpdate = Convert.ToDateTime(row["LastUpdate"]);

                //create a new purchInfo object and add ID to it
                purchInfo purchInfor = new purchInfo();
                purchInfor.purchID = Convert.ToInt32(row["FKpurchInfoID"]);

                //store lists of multiple rows fro each asset

                //access warrantyInfo for each info from sp
                //create SqlParameters for db query
                liSqlParams.Clear();
                SqlParameter aID = new SqlParameter("@assetID", newAsset.assetID);
                liSqlParams.Add(aID);

                //access db for all information
                
                dtResults = SendDataTableRequest("dbo.spGetWarrantyInfo", liSqlParams);

                //create warrantyInfoList variable
                List<warrantyInfo> warrantyInfoList = new List<warrantyInfo>();

                //store information in data members
                if (dtResults.Rows.Count > 0)
                {
                    foreach (DataRow row2 in dtResults.Rows)
                    {
                        //create warranty info to add to list
                        warrantyInfo warrantyInfo = new warrantyInfo();

                        //fill warrantyInfo with data
                        warrantyInfo.warrantyID = Convert.ToInt32(row2["PKwarrantyID"]);
                        warrantyInfo.startDate = Convert.ToDateTime(row2["StartDate"]);
                        warrantyInfo.endDate = Convert.ToDateTime(row2["EndDate"]);
                        warrantyInfo.coverage = row2["Coverage"].ToString();
                        warrantyInfo.phoneNum = Convert.ToInt32(row2["PhoneNum"]);
                        warrantyInfo.comments = row2["Comments"].ToString();

                        //store warrantyInfo into list
                        warrantyInfoList.Add(warrantyInfo);
                    }
                    newAsset.warrantyList = warrantyInfoList;
                }

                else
                {
                    //if no rows returned, set to null
                    newAsset.warrantyList = new List<warrantyInfo>();
                }

                //access maintenanceInfo for each info from sp
                //use same sql param list since still searching by assetID
                //access db for all information
                
                dtResults = SendDataTableRequest("dbo.spGetMaintenance", liSqlParams);

                //create maintenanceList variable
                List<maintenanceInfo> maintInfoList = new List<maintenanceInfo>();
                if (dtResults.Rows.Count > 0)
                {
                    //store information in data members
                    foreach (DataRow row3 in dtResults.Rows)
                    {
                        //create maint info to add to list
                        maintenanceInfo maintInfo = new maintenanceInfo();

                        //fill warrantyInfo with data
                        maintInfo.maintID = Convert.ToInt32(row3["PKmaintID"]);
                        maintInfo.date = Convert.ToDateTime(row3["Date"]);
                        maintInfo.price = Convert.ToInt32(row3["Price"]);
                        maintInfo.location = row3["location"].ToString();
                        maintInfo.comment = row3["Comment"].ToString();
                        maintInfo.company = row3["Company"].ToString();

                        //store warrantyInfo into list
                        maintInfoList.Add(maintInfo);
                    }
                    newAsset.maintenanceList = maintInfoList;
                }

                else
                {
                    //if no rows returned, fill w/ empty list
                    newAsset.maintenanceList = new List<maintenanceInfo>();
                }

                //access warrantyInfo for each info from sp
                //create SqlParameters for db query
                
                liSqlParams.Clear();
                SqlParameter pID = new SqlParameter("@purchInfoID", purchInfor.purchID);
                liSqlParams.Add(pID);

                //access db for all information
                
                dtResults = SendDataTableRequest("dbo.spGetPurchInfo", liSqlParams);

                //store information in data members
                if (dtResults.Rows.Count > 0)
                {
                    foreach (DataRow row4 in dtResults.Rows)
                    {
                        //create purchase info to add to list
                        

                        //fill purchInfo with data
                        purchInfor.date = Convert.ToDateTime(row4["Date"]);
                        purchInfor.cost = Convert.ToInt32(row4["Cost"]);
                        purchInfor.store = row4["Store"].ToString();
                        purchInfor.comments = row4["Comments"].ToString();

                        //store warrantyInfo into list
                        newAsset.purchInfo = purchInfor;
                    }
                }

                else
                {
                    //iff no rows returned, set to null
                    newAsset.purchInfo = new purchInfo();
                }
            //add newAsset to asset list
                newUserAssets.Add(newAsset);
            }
            
            //add list of assets to asset list obj
            newAssetList.userAssets = newUserAssets;

            
            //return datatable for use
            return newAssetList;
        }

    }
}
